<?php

require_once dirname(__FILE__) . '/../lib/patient_questionGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/patient_questionGeneratorHelper.class.php';

/**
 * patient_question actions.
 *
 * @package    docplus.co.za
 * @subpackage patient_question
 * @author     Fox Matuku
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class patient_questionActions extends autoPatient_questionActions
{

    public function preExecute()
    {
        $this->getUser()->setCurrentRoute("patient_question");
        parent::preExecute();
    }

    public function executeIndex(sfWebRequest $request)
    {
        $this->patient_question = new PatientQuestion();        
        if ($this->getUser()->isAuthenticated())
        {
            $this->patient_question->setUserId($this->getUser()->getDpUser()->getId());
        }
        $this->form = new PatientQuestionForm($this->patient_question);
    }

    public function executeShow(sfWebRequest $request)
    {
        $this->patient_question = $this->getRoute()->getObject();
        
        $patientQuestionAnswerComment = new PatientQuestionAnswerComment();        
        if ($this->getUser()->isAuthenticated())
        {
            $patientQuestionAnswerComment->setUserId($this->getUser()->getDpUser()->getId());
        }
        $this->form = new PatientQuestionAnswerCommentForm($patientQuestionAnswerComment);
    }

    public function executeNew(sfWebRequest $request)
    {
        $this->patient_question = new PatientQuestion();        
        if ($this->getUser()->isAuthenticated())
        {
            $this->patient_question->setUserId($this->getUser()->getDpUser()->getId());
        }
        $this->form = new PatientQuestionForm($this->patient_question);
    }

    public function executeCreate(sfWebRequest $request)
    {
        $this->form = $this->configuration->getForm();
        $this->patient_question = $this->form->getObject();

        $this->processForm($request, $this->form);
        $this->setTemplate('new');
    }

    protected function processForm(sfWebRequest $request, sfForm $form)
    {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid())
        {
            $notice = $form->getObject()->isNew() ? 'Thanks for your question!' : 'Thanks for your question!';

            try
            {
                $patient_question = $form->save();
            }
            catch (Doctrine_Validator_Exception $e)
            {

                $errorStack = $form->getObject()->getErrorStack();

                $message = get_class($form->getObject()) . ' has ' . count($errorStack) . " field" . (count($errorStack) > 1 ? 's' : null) . " with validation errors: ";
                foreach ($errorStack as $field => $errors)
                {
                    $message .= "$field (" . implode(", ", $errors) . "), ";
                }
                $message = trim($message, ', ');

                $this->getUser()->setFlash('error', $message);
                return sfView::SUCCESS;
            }

            $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $patient_question)));

            if ($request->hasParameter('_save_and_add'))
            {
                $this->getUser()->setFlash('notice', $notice . ' You can add another one below.');

                $this->redirect('@patient_question_new');
            }
            else
            {
                $this->getUser()->setFlash('notice', $notice);

                $this->redirect(array('sf_route' => 'patient_question_new', 'sf_subject' => $patient_question));
            }
        }
        else
        {
            $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
        }
    }

}
